package com.sihai.ehr.service.impl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.QueryCreator;
import com.sihai.ehr.model.biz.Attendance;
import com.sihai.ehr.service.AttendanceService;

/**
 * 考勤数据服务实现
 * 
 * @author henry
 * 
 */
@Service("attendanceService")
public class AttendanceServiceImpl implements AttendanceService {

	@Resource
	DataAccessor dataAccessor;

	@Override
	public Attendance get(Long id) {
		if (id == null) {
			return null;
		}
		return dataAccessor.get(Attendance.class, id);
	}

	@Override
	public Attendance getAttendanceByWorkdId(final Long workId,final int year,final int month) {
		if (workId == null || workId <= 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.findUnique(Attendance.class, new QueryCreator() {

				@Override
				public Query createQuery(Session session) {

					DynamicQuery dynamicQuery = new DynamicQuery("from Attendance a where 1 <> 2");
					dynamicQuery.appendIfNotNull("and a.employeeinfoWork.id =:workId", "workId", workId);
					dynamicQuery.appendIfNotNull("and a.attendYear =:attendYear", "attendYear", year);
					dynamicQuery.appendIfNotNull("and a.attendMonth =:attendMonth", "attendMonth", month);

					return dynamicQuery.build(session);
				}
			});
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}
